/* 
 * File:   string.c
 * Author: bjorn
 *
 * Created on September 3, 2007, 9:01 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "bstrlib.h"
#include "bstraux.h"
#include "rope.h"


size_t rope_size(Rope self) {
    return self->slen;
}

const char * rope_c(Rope self) {
    return (const char *) self->data;
}

Rope rope_make(const char * str) {
    return bfromcstr(str);
}


Rope rope_clone(const Rope self) {
    return bstrcpy(self);
}

Rope rope_free(Rope self) {
    bdestroy(self);
    return NULL;
}

Rope rope_make_join(const Rope self, const Rope other) {
    Rope result = rope_make("");
    bconcat(result, self);
    bconcat(result, other);
    return result;
}


Rope rope_cat(Rope self, const Rope other) {
    bconcat(self, other);
    return self;
}

Rope rope_cat_c(Rope self, const char * other) {
    bcatcstr(self, other);
    return self;
}

Rope rope_format(const char * fmt, ...) {
    int status;
    Rope result = rope_make("");
    bvformata(status, result, fmt, fmt);
    if (BSTR_OK == status) { return result; }
    rope_free(result);
    return NULL;
}


Ropelist ropelist_free(Ropelist self) {
    bstrListDestroy(self);
    return NULL;
}

Ropelist ropelist_make(int size) {
    Ropelist result   = bstrListCreate();
    int status          = bstrListAllocMin (result, size);   
    if( status == BSTR_OK) return result;
    ropelist_free(result);
    return NULL;
}

Ropelist rope_split_c(const Rope self, unsigned char splitchar) {
    return bsplit(self, splitchar);
}

Ropelist rope_split(const Rope self, const Rope splitstring) {
    return bsplitstr(self, splitstring);
}

Ropelist rope_split_any(const Rope self, const Rope splitstring) {
    return bsplits(self, splitstring);
}

size_t ropelist_size(Ropelist self) {
	return self->qty;
}

Rope ropelist_get(const Ropelist self, int index) {
    if(index >= self->qty) return NULL;
    return self->entry[index];    
}

Rope ropelist_set(const Ropelist self, int index, Rope value) {
    if(index >= self->qty) return NULL;
    return self->entry[index] = value;    
}



